package demo.cxf.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class TestServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public TestServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	 
	JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
	org.apache.cxf.endpoint.Client client = clientFactory.createClient("http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl");
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String qq = request.getParameter("qq");
			if(qq!=null&&!(qq=qq.trim()).isEmpty()){
				Object[] result = client.invoke("qqCheckOnline", qq);
				request.setAttribute("result", result[0]);
				request.setAttribute("qq", qq);
				System.out.println(qq+":"+result[0]);
			}
			request.getRequestDispatcher("/index.jsp").forward(request,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	 
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
		 
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		 
	}

}
